package com.bee.bootstrap.userRole.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bee.bootstrap.plugs.shiro.JwtRealm;
import com.bee.bootstrap.role.entity.Role;
import com.bee.bootstrap.role.service.IRoleService;
import com.bee.bootstrap.user.entity.User;
import com.bee.bootstrap.user.service.IUserService;
import com.bee.bootstrap.userRole.entity.UserRole;
import com.bee.bootstrap.userRole.mapper.UserRoleMapper;
import com.bee.bootstrap.userRole.service.IUserRoleService;
import com.bee.common.core.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liekkas
 * @since 2018-05-25
 */
@Service("userRoleService")
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Autowired
    private IRoleService roleServiceImpl;
    @Autowired
    private JwtRealm jwtRealm;
    @Autowired
    private IUserService userServiceImpl;

    @Override
    public Set<String> findRolesCodes(Long userId) {
        List<Role> roles=this.findRoles(userId);
        Set<String> roleCodes=new HashSet<>();
        roles.forEach(role->{
            roleCodes.add(role.getCode());
        });
        return roleCodes;
    }

    @Override
    public List<Role> findRoles(Long userId) {
        UserRole condition=new UserRole();
        condition.setUserId(userId);
        List<UserRole> userRoles=this.selectList(new EntityWrapper<>(condition));
        Set<Long> roleIds=new HashSet<>();
        userRoles.forEach(userRole -> {
            roleIds.add(userRole.getRoleId());
        });
        List<Role> roles=new ArrayList<>();
        if(roleIds.size()>0)
            roles=roleServiceImpl.selectList(new EntityWrapper<Role>().in("id",roleIds));
        return roles;
    }

    @Override
    public List<UserRole> findByRoleId(long roleId) {
        UserRole condition=new UserRole();
        condition.setRoleId(roleId);
        return this.selectList(new EntityWrapper<UserRole>(condition));
    }

    @Override
    public boolean deleteAllUserRole(Long userId) {
        List<Role> roles=this.findRoles(userId);
        List<Long> roleIds=new ArrayList<>();
        if(roles!=null&&roles.size()>0){
            roles.forEach(role -> {
                roleIds.add(role.getId());
            });
            return this.delete(new EntityWrapper<UserRole>().in("role_id",roleIds).eq("user_id",userId));
        }
        return false;
    }

    @Override
    public Result addUserRole(Set<String> roleCodes,Long userId) {
       List<Role> roles=roleServiceImpl.findRolesByCodes(roleCodes);
       User user=userServiceImpl.selectById(userId);
       if(roles!=null&&roles.size()>0){
           List<UserRole> userRoles=new ArrayList<>();
           roles.forEach(role -> {
                UserRole userRole=new UserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(role.getId());
                userRole.setCreateTime(new Date());
                userRoles.add(userRole);
           });
           this.insertBatch(userRoles);
           //刷新缓存
           jwtRealm.clearCache(user.getToken());
           return Result.success("保存用户角色成功");
       }
       return Result.fail("保存用户角色失败");
    }



}
